{include file="public/head"/}
<style type="text/css">
    .layui-table-cell{height: auto!important;}
    .layui-layer-tips{width: auto!important;}
    .goods_item{border-bottom: 1px dashed #008B8B;margin-bottom: 5px;padding-top: 5px;}
    .goods_item:last-child{border: none}
    .goods_item div span{padding: 3px;background: #F5F5F5}
    .goods_item div span:first-child{background:none}
    .goods_stance{display: inline-block;width: 30px}
    .goods_thumb{width:30px;height:30px;}
</style>
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">订单列表</div>
        <div class="layui-card-body layui-form">
            <div class="table-head-btn" style="margin-bottom: 10px;">
                <div class="layui-inline">
                    <input class="layui-input" name="key" id="key" placeholder="订单编号" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="order_sn" id="order_sn" placeholder="订单单号" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <select name="type" id="type">
                        <option value="">请选择类型</option>
                        {foreach $type_list as $item}
                        <option value="{$item.type}">{$item.title}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="activity_type" id="activity_type">
                        <option value="">请选择活动类型</option>
                        {foreach $goods_activity_type as $item}
                        <option value="{$item.type}">{$item.title}</option>
                        {/foreach}
                    </select>
                </div>

                <div class="layui-inline">
                    <input class="layui-input" name="user" id="user" placeholder="会员编号|注册手机" autocomplete="off">
                </div>

                <div class="layui-inline">
                    <select name="merch" id="merch" lay-search>
                        <option value="">请选择商家</option>
                        {foreach $merch_list as $item}
                        <option value="{$item.id}">编号：{$item.id} - {$item.title}</option>
                        {/foreach}
                    </select>
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <input class="layui-input" name="goods" id="goods" placeholder="商品编号|名称" autocomplete="off">
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <input class="layui-input" name="verify_code" id="verify_code" placeholder="核销码" autocomplete="off">
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <select name="pro_id" id="province" lay-filter="province">
                        <option value="">请选择省</option>
                        {volist name="province" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                    </select>
                </div>

                <div class="layui-input-inline unimportant layui-hide" >
                    <select name="city_id" id="city" lay-filter="city">
                        <option value="">请选择市</option>
                    </select>
                </div>

                <div class="layui-input-inline unimportant layui-hide">
                    <select name="area_id" id="area" lay-filter="area">
                        <option value="">请选择县/区</option>
                    </select>
                </div>

                <div class="layui-input-inline unimportant layui-hide">
                    <select name="is_change">
                        <option value="">请选择是否已换货</option>
                        <option value="0">未换货</option>
                        <option value="1">已换货</option>
                    </select>
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <select name="pay_type" id="pay_type">
                        <option value="">请选择支付方式</option>
                        {volist name="pay_type" id="vo"}
                        <option value="{$vo.type}">{$vo.title}</option>
                        {/volist}
                    </select>
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <select name="time_type" id="time_type">
                        <option value="">请选择筛选时间</option>
                        <option value="create_time">下单时间</option>
                        <option value="pay_time">支付时间</option>
                        <option value="send_time">发货时间</option>
                        <option value="receive_time">收货(完成)时间</option>
                        <option value="refund_apply_time">申请退款时间</option>
                        <option value="refund_time">退款时间</option>
                        <option value="change_apply_time">申请换货时间</option>
                        <option value="change_time">换货时间</option>
                    </select>
                </div>

                <div class="layui-inline unimportant layui-hide">
                    <input type="text" name="start_time" class="layui-input" id="start_time"
                           placeholder="开始时间">
                </div>
                <div class="layui-inline unimportant layui-hide">
                    <input type="text" name="end_time" class="layui-input" id="end_time"
                           placeholder="结束时间">
                </div>



                <button class="layui-btn" data-type="reload">搜索</button>
                <button class="layui-btn" data-type="export">导出</button>
                <button class="layui-btn layui-btn-normal" data-type="condition" id="condition_btn">更多条件</button>
            </div>
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}

<script type="text/html" id="type_info">
    {{d.type_html}}
</script>

<script type="text/html" id="activity_type_info">
    {{d.activity_type_html}}
</script>

<script type="text/html" id="pro">
    {{d.pro}}-{{d.city}}-{{d.area}}
</script>

<script type="text/html" id="goods_con">
    <ul>
        {{#  layui.each(d.order_goods, function(index, item){ }}
        <li class="goods_item">
            <div>
                <span><img src="{{item.thumb}}" class="goods_thumb" onclick="layer.tips('<div style=\'background-color: #fff;\'><img style=\'max-width: 300px;\' src={{item.thumb}}></div>',this,{tips: [1, '#fff'],time:-1,closeBtn:1,area: ['auto', 'auto']});"></span>
                <span>编号：{{ item.goods_id }}</span>
                <span>名称：{{ item.short_title }}</span>
            </div>
            <div>
                <span class="goods_stance"></span>
                <span>规格：{{ item.spec_item_title }}</span>
                <span>价格：{{ item.price }}</span>
                <span>数量：{{ item.num }}</span>
            </div>
        </li>
        {{#  }); }}
        {{#  if(d.order_goods.length === 0){ }}
        无数据
        {{#  } }}
    </ul>
</script>

<script type="text/html" id="merch_con">
    <p>编号：{{d.merch_id}}</p>
    <p>名称：{{d.merch_title}}</p>
</script>

<script type="text/html" id="user_con">
    <p>编号：{{d.uid}}</p>
    <p>昵称：{{d.nickname}}</p>
    <p>注册手机：{{d.user_mobile}}</p>
</script>

<script type="text/html" id="order_con">
    <p>编号：{{d.order_id}}</p>
    <p>单号：{{d.order_sn}}</p>
</script>

<script type="text/html" id="pay_type_con">
    {{# if(d.status!=0 && d.status!=-1){ }}
    <p>支付方式：{{ d.pay_type_html }}</p>
    <p>支付金额：{{ d.pay_price }}</p>
    <p>支付单号：{{ d.transaction_id }}</p>
    {{# } }}
</script>

<script type="text/html" id="price">
    <p>商品总价：{{ d.goods_price }}</p>
    <p>运费：{{ d.dispatch_price }}</p>
    <!--Totag 隐藏积分抵扣-->
    <!--    <p>抵扣：{{ d.coin }}{:config('self.user_coin_title')}抵扣{{ d.coin_deduct }}</p>-->
    <p>订单金额：{{ d.order_price }}</p>
    <p>商家结算：{{ d.merch_price }}</p>
</script>

<script type="text/html" id="transaction_id_bor">
    {{# if(d.status>=1||d.status<=-2){ }}
    {{d.transaction_id}}
    {{# } }}
</script>


<script type="text/html" id="action">

    <p><a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">详情</a></p>

    {{# if(d.status==1){ }}
    {{# if(d.type==1){ }}
    <p><a class="layui-btn layui-btn-xs" lay-event="doVerify">确认核销</a></p>
    {{# }else{ }}
    <p><a class="layui-btn layui-btn-xs" lay-event="doSend">确认发货</a></p>
    {{# } }}
    {{# } }}

    {{# if(d.status==2){ }}
    {{# if(d.type==2){ }}
    <p><a class="layui-btn layui-btn-xs" lay-event="doSend">修改发货</a></p>
    <p><a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="doReceive">确认收货</a></p>
    {{# } }}
    {{# } }}

    {{# if(d.type==2 && (d.status>=2 || d.old_status>=2)){ }}
    <p><a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="sendInfo">查看物流</a></p>
    {{# } }}

    {{# if(d.type==1 && (d.status>=1 || d.old_status>=1)){ }}
    <p><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="verifyInfo">核销记录</a></p>
    {{# } }}

    {{# if(d.status==-2){ }}
    <p><a class="layui-btn layui-btn-xs" lay-event="onlineRefund">线上退款</a></p>
    <p><a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="offlineRefund">线下退款</a></p>
    <p><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="rejectRefund">驳回退款</a></p>
    {{# } }}

    {{# if(d.status==-4){ }}
    <p><a class="layui-btn layui-btn-xs" lay-event="confirmChange">确认换货</a></p>
    <p><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="rejectChange">驳回换货</a></p>
    {{# } }}

    {{# if(d.status==-1){ }}
    <p><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a></p>
    {{# } }}

</script>
<script type="text/html" id="time_con">
    <p>下单：{{d.create_time}}</p>
    {{# if(d.status!=0 && d.status!=-1){ }}
    <p>支付：{{d.pay_time}}</p>
    {{# } }}
    {{# if(d.type==2 && (d.status>=2 || d.old_status<=-2)){ }}
    <p>发货：{{d.send_time}}</p>
    {{# } }}
    {{# if(d.status>=3){ }}
    <p>收货：{{d.receive_time}}</p>
    {{# } }}
    {{# if(d.refund_apply_time!=''){ }}
    <p>申请退款：{{d.refund_apply_time}}</p>
    {{# } }}
    {{# if(d.refund_time!=''){ }}
    <p>退款：{{d.refund_time}}</p>
    {{# } }}
    {{# if(d.change_apply_time!=''){ }}
    <p>申请换货：{{d.change_apply_time}}</p>
    {{# } }}
    {{# if(d.change_time!=''){ }}
    <p>换货：{{d.change_time}}</p>
    {{# } }}
    {{# if(d.cancel_time!=''){ }}
    <p>取消：{{d.cancel_time}}</p>
    {{# } }}
</script>
<script>
    layui.config({
        base: '__LAYADMIN__/',
        version: 20220407
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['table', 'form','laydate'],function (){
        var $ = layui.$, admin = layui.admin, table = layui.table, form = layui.form, laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start_time', //指定元素
            type: 'datetime'
        });
        laydate.render({
            elem: '#end_time', //指定元素
            type: 'datetime'
        });

        laydate.render({
            elem: '#start_time2', //指定元素
            type: 'datetime'
        });
        laydate.render({
            elem: '#end_time2', //指定元素
            type: 'datetime'
        });

        laydate.render({
            elem: '#start_time3', //指定元素
            type: 'datetime'
        });
        laydate.render({
            elem: '#end_time3', //指定元素
            type: 'datetime'
        });

        //全部数据
        tableIn = table.render({
            elem: '#list',
            url: '{:url(\'Order/index\',[\'status\'=>$status])}',
            method: 'post',
            toolbar: '',
            defaultToolbar:[],
            autoSort: false,
            height: 'full-150',
            resize:true,
            cols: [[
                {field: 'id', title: '编号', width: 80,fixed:true, sort:true},
                {field: 'order_sn', title: '订单单号', align:"center", width: 230},
                {field: 'type', title: '订单类型', align:"center", width: 100,templet:'#type_info'},
                {field: 'activity_type', title: '活动类型', align:"center", width: 100,templet:'#activity_type_info'},
                {field: 'goods_num', title: '商品总数', width: 110,align:"center",sort:true},
                {field: 'goods', title: '商品', width: 300,templet:'#goods_con'},
                {field: 'verify_code', title: '核销码', width: 110,align: "center"},
                {field: 'order_price', title: '订单价格', width: 190,templet: '#price'},
                {field: 'status_str',align:"center", title: '状态', width: 160},
                {field: 'user', title: '会员', width: 180,templet:'#user_con'},
                {field: 'merch', title: '商家', width: 180,templet:'#merch_con'},
                {field: 'pro_id', title: '收货地区', width: 200,templet:'#pro'},
                {field: 'pay_type', title: '支付方式', width: 200,templet: '#pay_type_con'},

                {field: 'create_time', title: '时间', align:"center", width: 220,templet: '#time_con'},
                {width: 150,title:'操作', align: 'center',fixed:'right', toolbar: '#action'}
            ]],
            page: true,
            done: function (res, curr, count) {
                $(".layui-table-header tr").each(function (index, val) {
                    $(".layui-table-fixed").each(function () {
                        $($(this).find(".layui-table-header thead tr")[index]).height($(val).height());
                    });
                });
                $(".layui-table-main tr").each(function (index, val) {
                    $(".layui-table-fixed").each(function () {
                        $($(this).find(".layui-table-body tbody tr")[index]).height($(val).height());
                    });
                });
            }
        });

        //监听排序事件
        table.on('sort(list)', function(obj){
            table.reload('list', {
                initSort: obj
                ,where: {sort_by: obj.field,sort_order: obj.type}
            });
        });


        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            var id = data.id;
            var order_sn = data.order_sn;
            if(obj.event === 'detail'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/detail\')}?id=' + id,
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
            }else if(obj.event === 'del'){
                layer.confirm('确定删除此订单吗？删除后不可恢复', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/del\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'doSend'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/doSend\')}?id=' + id,
                    area: ['50%', '80%'],
                    maxmin: true
                });
                //layer.full(index);
            }else if(obj.event === 'doReceive'){
                layer.confirm('确定订单已收货吗？操作不可逆转', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/doReceive\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'sendInfo'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/sendInfo\')}?id=' + id,
                    area: ['50%', '80%'],
                    maxmin: true
                });
                //layer.full(index);
            }else if(obj.event === 'doVerify'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/doVerify\')}?id=' + id,
                    area: ['50%', '80%'],
                    maxmin: true
                });
                //layer.full(index);
            }else if(obj.event === 'verifyInfo'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/verifyInfo\')}?order_id=' + id+'&order_sn='+order_sn,
                    area: ['70%', '80%'],
                    maxmin: true
                });
                //layer.full(index);
            }else if(obj.event === 'rejectRefund'){
                layer.confirm('确定驳回退款吗？操作不可逆转', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/rejectRefund\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'offlineRefund'){
                layer.confirm('确定线下退款吗？此操作仅更改退款状态，金额请线下联系买家退款', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/offlineRefund\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {

                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'onlineRefund'){
                layer.confirm('确定线上退款吗？买家支付金额将原路退回', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/onlineRefund\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'rejectChange'){
                layer.confirm('确定驳回换货吗？操作不可逆转', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'Order/rejectChange\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }else if(obj.event === 'confirmChange'){
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'Order/confirmChange\')}?id=' + id,
                    area: ['50%', '80%'],
                    maxmin: true
                });
                //layer.full(index);
            }
        });

        //地区联动
        form.on('select(province)', function (data) {
            var pid = data.value;
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.get("{:url(\'Utils/getRegion\')}?pid=" + pid, function (data) {
                layer.close(loading);
                var html = '<option value="">请选择市</option>';
                $.each(data, function (i, value) {
                    html += '<option value="' + value.id + '">' + value.name + '</option>';
                });
                $('#city').html(html);
                $('#area').html('<option value="">请选择县/区</option>');
                form.render()
            });
        });

        form.on('select(city)', function (data) {
            var pid = data.value;
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.get("{:url('Utils/getRegion')}?pid=" + pid, function (data) {
                layer.close(loading);
                var html = '<option value="">请选择县/区</option>';
                $.each(data, function (i, value) {
                    html += '<option value="' + value.id + '">' + value.name + '</option>';
                });
                $('#area').html(html);

                form.render()
            });
        });

        //监听工具条
        table.on('toolbar(list)', function (obj) {
            if(obj.event=='direct'){

            }
        });

        var active = {
            reload: function () {
                var search = form.getValue(null,$('.layui-form .table-head-btn'));

                //执行重载
                table.reload('list', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: search
                });
            },
            export: function () {
                var search = form.getValue(null,$('.layui-form .table-head-btn'));
                let w = Object.entries(search);
                w.forEach((v,i)=>{w[i]=v.join('=')});
                var export_str = w.join('&');
                var url ='{:url(\'Order/export\')}?status={$status}&'+export_str;
                window.open(url,'_blank');
            },
            condition: function () {
                var txt = $("#condition_btn").text();
                if(txt=='更多条件'){
                    table.reload('list', {
                        height: 'full-220' // 设置新的高度
                    });
                    $(".unimportant").removeClass('layui-hide');
                    $("#condition_btn").text('收起条件');
                }else{
                    table.reload('list', {
                        height: 'full-150' // 设置新的高度
                    });
                    $(".unimportant").addClass('layui-hide');
                    $("#condition_btn").text('更多条件');
                }

            }
        };
        $('.table-head-btn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>